package com.solcoo.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.solcoo.json.MerchantInfoJson;
import com.solcoo.json.MerchantSimpleInfoJson;
import com.solcoo.pojo.YongHu;

@Repository
public class YongHuDao {

	@Resource
	private SqlMapClient sqlMapClient;
	
    private static final String INSERT_USER = "insertUser"; // 插入用户，返回id
    private static final String UPDATE_USER_INFO= "updateUserInfo"; // 根据用户名码更新用户
    private static final String SELECT_USER_COUNT_BY_USER_NAME = "selectUserCountByUserName"; // 根据用户名查询用户
    private static final String ADD_MEMBER_SHIP = "addMembership";//用户添加商户信息
    private static final String QUERY_MERCHANT_LIST = "queryMerchantList";//商户列表
    private static final String DECREASE_MEMBER_SHIP = "decreaseMembership";//减少用户

    public int addYongHu(YongHu yongHu) throws SQLException {
        sqlMapClient.insert(INSERT_USER,yongHu);
        return yongHu.getId();
    }

    public boolean update(YongHu yongHu) throws SQLException {
        return sqlMapClient.update(UPDATE_USER_INFO, yongHu) >= 1 ? true : false;
    }

    // 根据手机号 查询用户
    public YongHu selectUserByPhone(String shoujih) throws SQLException {
        return (YongHu) sqlMapClient.queryForObject("selectUserByPhone", shoujih);
    }

    // 根据类型查询用户列表
    public List<YongHu> queryListByLx(String leixing) throws SQLException {
        return (List<YongHu>)sqlMapClient.queryForList("selectListByLx", leixing);
    }

    // 根据昵称查找用户
    public YongHu selectUserByNicheng(String nicheng) throws SQLException {
        return (YongHu) sqlMapClient.queryForObject("selectUserByNicheng", nicheng);
    }


    public YongHu selectUserByYonghum(String yonghum) throws SQLException {
        return (YongHu) sqlMapClient.queryForObject(SELECT_USER_COUNT_BY_USER_NAME, yonghum);
    }

    public YongHu selectUserByDianMing(String dianming) throws SQLException {
        return (YongHu) sqlMapClient.queryForObject("selectUserByDianming", dianming);
    }

    public boolean addMemberCount(Integer merchantId) throws SQLException {

        return sqlMapClient.update(ADD_MEMBER_SHIP,merchantId) ==1 ? true : false;
    }

    public boolean decreaseMemberCount(Integer merchantId) throws SQLException{
        return sqlMapClient.update(DECREASE_MEMBER_SHIP,merchantId) == 1 ?true : false;
    }

    public List<MerchantSimpleInfoJson> getMerchantSimpleInfos(String merchantType)
            throws SQLException {
        return sqlMapClient.queryForList(QUERY_MERCHANT_LIST, merchantType);
    }
    
    public List<MerchantInfoJson> getMerchantList(String merchantType) throws SQLException {
        return sqlMapClient.queryForList("getMerchantList", merchantType);
    }
    
	public boolean getUserByPhone(String phone) throws Exception {
		int count = (Integer) sqlMapClient.queryForObject("getUserByPhone", phone);
		
		return count > 0 ? true : false;
	}

	public boolean getUserByPhoneAndPassword(String phone, String password) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		
		map.put("phone", phone);
		map.put("password", password);
		
		int count = (Integer) sqlMapClient.queryForObject("getUserByPhoneAndPassword", map);
		
		return count > 0 ? true : false;
	}
	
	public YongHu getUser(String phone, String password) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		
		map.put("phone", phone);
		map.put("password", password);
		
		return (YongHu) sqlMapClient.queryForObject("getUser", map);
	}

	public Integer getMemberCountByShanhuid(int shanghuid) throws Exception {
		return (Integer) sqlMapClient.queryForObject("getMemberCountByShanhuid", shanghuid);
	}

	public void updatePasswd(String telephone, String newPasswd) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("telephone", telephone);
		map.put("newPasswd", newPasswd);
		
		sqlMapClient.update("updatePasswd", map);
	}
	
	public void updateQrcode(String telephone, String qrcode) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("telephone", telephone);
		map.put("qrcode", qrcode);
		
		sqlMapClient.update("updateQrcode", map);
	}
}
